package io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringReader;

public class IOUtils {

	
	/*
	 * InputStream类型
	 * 
	 * ByteArrayInputStream,
	 * StringBufferInputStream,
	 * FileInputStream,
	 * PipedInputStream,
	 * SequenceInputStream,
	 * FilterInputStream(装饰器接口,带缓存):DataInputStream,BufferedInputStream,LineNumberInputStream,PushbackInputStream
	 */
	
	/*
	 * OutputStream类型
	 * 
	 * ByteArrayOutputStream,
	 * FileOutputStream,
	 * PipedOutputStream,
	 * FilterOutputStream(装饰接口,带缓存):DataOutputStream,BufferedOutputStream,PrintStream
	 */
	
	/*
	 * 适配器(adapter):字节字符转换
	 * InputStreamReader,OutputStreamWriter
	 */
	
	/*
	 * Java 1.0类  对应 Java 1.1类(字节和字符)
	 * InputStream,Reader
	 * OutputStream,Writer
	 * FileInputStream,FileReader
	 * FileOutputStream,FileWriter
	 * StringBufferInputStream,StringReader,StringWriter
	 * ByteArrayInputStream,CharArrayReader
	 * ByteArrayOutputStream,CharArrayWriter
	 * PipedInputStream,PipedReader
	 * PipedOutputStream,PipedWriter
	 */
	
	/*
	 * 过滤器：
	 * 
	 * FilterInputStream,FilterReader
	 * FilterOutputStream,FilterWriter
	 * BufferedInputStream,BufferedReader
	 * BufferedOutputStream,BufferedWriter 
	 * DataInputStream,DataInputStream(要readLine()时,使用BufferedReader)
	 * PrintStream,PrintWriter
	 */
	
	/**
	 * read()是以int形式返回下一字节，必出转型为char才能正确打印
	 * 
	 * 格式化数据：DataInputStream
	 * 
	 * 
	 */
	
	
	
	/*
	 * 1.缓冲输入文件
	 */	
	public static String read(String fileName) throws IOException {
		
		BufferedReader in = new BufferedReader(new FileReader(fileName));
		String s = "";
		StringBuilder sb = new StringBuilder();
		while((s = in.readLine()) != null){
			sb.append(s + "\n");
		}
		in.close();
		
		return sb.toString();
	}
	
	public static void util(){
		try {
			//2.从内存输入
			StringReader inReader = new StringReader(new IOUtils().read("fileName"));
			
			//3.格式化的内存输入
			DataInputStream inDataInputStream = new DataInputStream(
					new ByteArrayInputStream(
							new IOUtils().read("fileName").getBytes()));
			
			//4.基本的文件输出
			BufferedReader inBufferedReader = new BufferedReader(
					new StringReader(
							new IOUtils().read("fileName")));
			PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("file")));
			int lineCount = 1;
			String string;
			while ((string = inBufferedReader.readLine()) != null) {
				out.println(lineCount++ + ":" + string);
			}
			out.close();
			
			//5.存储和恢复数据
			DataOutputStream outputStream = new DataOutputStream(
					new BufferedOutputStream(new FileOutputStream("file")));
			outputStream.writeDouble(3.1415926);
			outputStream.writeUTF("That was pi");
			outputStream.writeDouble(12.34);
			out.close();
			DataInputStream inputStream = new DataInputStream(new BufferedInputStream(
					new FileInputStream("file")));
			System.out.println(inputStream.readUTF());
			
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


}
